<template>
  <div v-loading="loading" class="list-table noPad">
    <el-tabs v-model="activeName" @tab-click="clickMenu">
      <el-tab-pane v-for="(item, key) in childMenuTabs" :key="key" :lazy="true" :label="$t(item.menuName)" :name="item.menuName" />
    </el-tabs>
    <div v-loading="loading" class="voteMain">
      <!--动态页面 ，根据is的值来确定渲染哪个组件 -->
      <component :is="menuPath" ref="menuPath" :parameter="parameter" />
    </div>
  </div>
</template>
<script>
import { childMenuList	} from '@/api/system/menu'
import { getAsyncComponent } from '@/utils'
import { getDealBase } from '@/api/base/deal/dealBase'
import merge from 'webpack-merge'

export default {
  data() {
    return {
      // 加载
      loading: true,
      // 选中页签名称
      activeName: this.$t('基本信息'),
      // 项目ID
      dealId: this.$route.query.id,
      // 子页签
      childMenuTabs: [],
      // 组件地址
      menuPath: '',
      // 参数
      parameter: {},
      // 菜单标识
      menuIdent: 'bf_info_tab_CB'
    }
  },
  created() {
    this.getDealBfBase()
  },
  mounted() {
    this.listComponent()
  },
  methods: {
    listLoad() {
      this.loading = true
      // 菜单列表
      childMenuList(this.menuIdent).then(response => {
        this.childMenuTabs = response.rows
        this.clickMenu(response.rows[0])
        this.loading = false
      })
    },
    clickMenu(tab) {
      this.childMenuTabs && this.childMenuTabs.forEach(item => {
        if (item.menuName === tab.menuName || item.menuName === tab.name) {
          this.$nextTick(() => {
            this.$set(item, 'flag', true)
            this.importFormPath(item.component)
            this.listComponent()
          })
        } else {
          this.$set(item, 'flag', false)
        }
      })
    },
    listComponent() {
      this.$bus.$once('async_done', () => {
        this.load()
      })
    },
    load() {
      if (this.$refs.menuPath?.init !== undefined) {
        const data = {
          id: this.dealId
        }
        this.$refs.menuPath.init(data)
        this.$refs.menuPath.openLocation = 'Home'
      }
    },
    // 是否项目表单配置路径
    isOnlConfig(menuPath) {
      var dealType = sessionStorage.getItem('dealBaseFormDealType' + this.$route.query.id)
      var dealBaseFormFlag = localStorage.getItem('dealBaseFormFlag')
      var dealBaseFormTempletId = localStorage.getItem('dealBaseFormTempletId')
      var dealTypes = localStorage.getItem('configFormDealType')
      if (menuPath && menuPath.includes('deal/base/dealBaseForm') && dealTypes.indexOf(dealType) !== -1 && dealBaseFormFlag === 'configForm') {
        const newKey = menuPath.split('?')[1]
        menuPath = 'system/desform/release/onlineTemplate?queryId=' + dealBaseFormTempletId + '&type=deal'
        if (newKey) {
          menuPath = menuPath + '&' + newKey
        }
      }
      return menuPath
    },
    // 引入组件
    importFormPath(menuPath) {
      if (menuPath) {
        // 是否设计表单路径
        this.whetherForm = menuPath.indexOf('dev/form/base/form') > -1
        menuPath = this.isOnlConfig(menuPath)
        // 是否带参数
        if (menuPath.lastIndexOf('?') > -1) {
          const index = menuPath.lastIndexOf('?')
          const sMenuPath = menuPath.substring(0, index)
          this.menuPath = getAsyncComponent('@/views/' + sMenuPath)
          // 增加参数
          const newKey = menuPath.split('?')[1]
          var newstr = newKey.replace(/=/g, '":"')
          this.parameter = JSON.parse('{"' + newstr.replace(/&/g, '","') + '"}')
          // 地址栏添加左侧菜单地址
          this.$router.push({
            query: merge(this.$route.query, this.parameter)
          })
        } else {
          this.menuPath = getAsyncComponent('@/views/' + menuPath)
        }
      } else {
        const error = 'error/404'
        this.importFormPath(error)
      }
    },
    // 获取参股基金
    getDealBfBase() {
      getDealBase(this.dealId).then(res => {
        if (res.data.dealStatus !== null && res.data.dealStatus !== '') {
          this.menuIdent = 'bf_info_tab_' + res.data.dealStatus
        }
        this.listLoad()
      })
    }
  }
}
</script>
